package com.dyb.demo.java8;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author dyb
 * @Description
 * @date 2019/3/15 14:49
 */
public class J8_Stream {

    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
//        test4();
//        test5();
        test6();
    }

    private static void test2 () {
        Stream.of("Apsara", "2019", "9.25 - 9.27", "Digital Economy")
                .map(x -> 57 == x.charAt(0) ? "Here is the" : 50 == x.charAt(0) ? x.concat(":") : x)
                .reduce((x, y) -> x + " " + y)
                .ifPresent(System.out::print);
    }

    public static void test1() {
        List<String> list = Arrays.asList("a", "b", "g", "d", "e", "f");
        // filter 方法用于通过设置的条件过滤出元素
        list.stream().filter(s -> !s.isEmpty()).filter(s->!s.equals("e")).forEach(System.out:: println);
        // map 方法用于映射每个元素到对应的结果
        list.stream().map(i -> "map->"+i).forEach(System.out::println);
        // limit 返回 Stream 的前面 n 个元素；skip 则是扔掉前 n 个元素任何基类可以出现的地方，子类一定可以出现。
        list.stream().limit(5).map(s-> "limit->"+s).forEach(System.out:: println);
        list.stream().skip(5).map(s->"skip->"+s).forEach(System.out:: println);
        // sorted 方法用于对流进行排序
        list.stream().sorted().forEach(System.out::print);

        List<Integer> numbers = Arrays.asList(1,2,3,5,4,5,6,7);
        System.out.println();
        numbers.stream().map(i -> i*i).forEach(System.out::print);
        // distinct主要用来去重,后续重复的不再显示
        numbers.stream().distinct().forEach(System.out::print);
    }

    /**
     * list -> map
     */
    public static void test3() {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "一");
        map.put(2, "二");
        map.put(3, "三");
        map.put(4, "四");
        List<Map> list = Collections.singletonList(map);
        Map list2map = list.stream().collect(Collectors.toMap(Map::keySet, Map::values));
    }

    /**
     * map -> list
     */
    public static void test4() {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "一");
        map.put(2, "二");
        map.put(3, "三");
        map.put(4, "四");

        List map2list2 = map.entrySet().stream().peek(m -> m.getValue()).collect(Collectors.toList());
        printList(map2list2);

    }

    private static void printList(List list) {
        list.forEach(System.out::println);
    }

    private static void printMap(Map map) {
        map.keySet().stream().map(k -> "["+ k +", "+ map.get(k) +"]").forEach(System.out::println);
    }


    static class User {
        private Integer id;
        private String name;

        public User(Integer id, String name) {
            this.id = id;
            this.name = name;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    /**
     * 一般常用方法
     */
    public static void test5() {
        User user1 = new J8_Stream.User(5, "aa");
        User user2 = new User(2, "ba");
        User user3 = new User(3, "ac");
        User user4 = new User(1, "aad");
        User user5 = new User(4, "asd");
        List<User> userList = Arrays.asList(user1, user2, user3, user4, user5);
        printList(userList);
        //获取所有ID集合
        List<Integer> idList = userList.stream().map(User::getId).collect(Collectors.toList());
        printList(idList);
        //排序ID
        List<User> userList1 = userList.stream().sorted(Comparator.comparingInt(User::getId)).collect(Collectors.toList());
        printList(userList1);
        //分组
        Map<String, Long> map = userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.counting()));
        printMap(map);
        //获取id大于2的
        List<User> userList2 = userList.stream().filter(user -> user.getId() > 2).collect(Collectors.toList());
        //获取最大
        Integer id = userList.stream().map(User::getId).max(Integer::compareTo).get();
        //获取最小
        Integer id1 = userList.stream().map(User::getId).min(Integer::compareTo).get();
        //获取id数量
        long count = userList.stream().map(User::getId).count();
        //总和
        int sum = userList.stream().mapToInt(User::getId).sum();
        //获取平均值
        double d = userList.stream().mapToInt(User::getId).average().getAsDouble();
        //匹配findAny/allMatch/noneMatch多用来判断
        User user = userList.stream().findAny().get();
        //获取第一个对象
        User user6 = userList.stream().findFirst().get();
        //将名字全转换为大写
        List<String> list = userList.stream().map(User::getName).map(String::toUpperCase).collect(Collectors.toList());
        //获取忽略第一个并取前几条数据
        List<User> list1 = userList.stream().skip(1).limit(2).collect(Collectors.toList());
        //去重
        List<User> collect = userList.stream().distinct().collect(Collectors.toList());
    }

    public static void test6() {
        User user1 = new J8_Stream.User(5, "aa");
        User user2 = new User(2, "ba");
        User user3 = new User(3, "ac");
        User user4 = new User(1, "aad");
        User user5 = new User(4, "asd");

        List<User> userList = Arrays.asList(user1, user2, user3, user4, user5);
        printList(userList);
        //获取id大于2的
        List<User> userList2 = userList.stream().filter(user -> user.getId() > 5).collect(Collectors.toList());
        printList(userList2);
    }
}
